/**
    Copyright (C) 2012 Guido Ludwig

    This file is part of JATT.
	http://code.google.com/p/jatt/

    JATT is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.guidoludwig.jatt.chain.commands;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;

import de.guidoludwig.jatt.JATT;
import de.guidoludwig.jatt.chain.JATTContext;
import de.guidoludwig.jatt.chain.commands.config.JATTCommandConfig;

/**
 * @author Guido Ludwig
 */
public abstract class JATTCommand<T extends JATTCommandConfig> implements Command {

    private T config;

    public JATTCommand() {
    	// default
    }

    public JATTCommand(T config) {
        this.config = config;
    }

    public T getConfig(){
        return config;
    }
    public void configure(T config) {
    	this.config = config;
    }
    public boolean isConfigured() {
    	return config != null;
    }

    @Override
    public final boolean execute(Context context) throws Exception {
    	if (!isConfigured()) {
			JATT.error(new IllegalStateException(getClass().getName() + " not configured"));
		}
    	JATTContext jcontext = (JATTContext)context;
        if (!config.isActive()) {
        	JATT.debug(config.getTitle() + " not active.");
            return doneIfNotActive();
        }
        JATT.debug(config.getTitle() + " executing.");
        return execute0(jcontext);
    }

    protected abstract boolean execute0(JATTContext context);


    protected boolean doneIfNotActive() {
        return false;
    }

}
